import {ref} from "vue";
import service from "@/api/logistics/service.js";
import commonApi from "@/api/common.js";

const declareColumns = {
    parcelList: [],
    originalColumns: [
        {title: 'PO Number', data: 'po_number'},
        {title: '重量(KG)', data: 'client_weight'},
        {title: '长(CM)', data: 'client_length',},
        {title: '宽(CM)', data: 'client_width',},
        {title: '高(CM)', data: 'client_height',},
    ],
    baseDeclareColumns: [
        {title: 'PO Number', data: 'po_number', dataIndex: 'po_number', align: 'center',},
        {title: '中文品名', data: 'cn_name', dataIndex: 'cn_name', align: 'center',},
        {title: '英文品名', data: 'en_name', dataIndex: 'en_name', align: 'center',},
        {title: '申报单价', data: 'price', dataIndex: 'price',},
        {title: '数量', data: 'quantity', dataIndex: 'quantity', align: 'center',},
        {title: '材质', data: 'material', dataIndex: 'material', align: 'center',},
    ],
    declareList: [
        'cn_name',
        'en_name',
        'price',
        'quantity',
        'material',
    ],
    clientList: [
        'ext_number',
        'po_number',
        'client_weight',
        'client_width',
        'client_height',
        'client_length',
    ],
    declareMap: ref([]),
    getDeclareMap: async function () {
        if (this.declareMap.value.length) {
            return
        }
        console.log(this.declareMap)
        const declareRes = await commonApi.getDict('declaration')
        this.declareMap.value = declareRes.data.reduce((acc, cur) => {
            acc[cur.key] = cur.title
            return acc
        }, {})
    },
    getDeclareColumns: async function (service_id) {
        const serviceConfig = await service.read(service_id)
        console.log(serviceConfig)
        this.parcelList = serviceConfig.data.parcel
        await this.getDeclareMap();
        console.log(this.parcelList)
        return this.parcelList;
        let originalColumns = [];
        for (const [k,i] of Object.entries(this.parcelList)) {
            originalColumns.push({
                title: this.declareMap.value[k],
                data: k
            })
        }
        return originalColumns
    },
    formatDeclareColumns(parcelList,type = 'add') {
        let columns = [];
        switch (type) {
            case 'add':
                columns = JSON.parse(JSON.stringify(this.originalColumns))
                columns.unshift({title: '箱号', data: 'number'})
                break;
            case 'create':
                columns = JSON.parse(JSON.stringify(this.originalColumns))
                columns.unshift({title: '箱号', data: 'ext_number'})
                break;
            case 'clearance':
                columns = JSON.parse(JSON.stringify(this.baseDeclareColumns))
                break;
            case 'view':
                columns = JSON.parse(JSON.stringify(this.baseDeclareColumns))
                break;
        }
        let objList = ['view','clearance'].indexOf(type) > -1 ? this.declareList : this.clientList
        if(this.parcelList){
            for (const [k, i] of Object.entries(this.parcelList)) {
                let clientIndex = objList.indexOf(k)
                if (clientIndex > -1) {
                    columns[clientIndex].required =  i === 1
                    continue;
                }
                if(['view','clearance'].indexOf(type) > -1 && this.clientList.indexOf(k) > -1 ){
                    continue;
                }
                columns.push({
                    title: this.declareMap.value[k],
                    data: k,
                    dataIndex: k,
                    isDeclare: true,
                    required: i === 1
                })
            }
        }
        
        return columns;
    }
}
export default declareColumns